using System;
using System.Collections.Generic;
using System.Text;
using Rapier.Kodachi.Shared;
using Microsoft.Win32;
using System.Xml.Serialization;
using System.IO;
//using System.Windows.Input;

namespace Rapier.Kodachi.Core
{
    public class CorePlugin : Plugin
    {
        #region Properties
        public override string Author
        {
            get { return "leeb"; }
        }

        public override string Url
        {
            get { return "http://www.rapiersoftware.com"; }
        }

        private static CoreSettings _CoreSettings;
        public static CoreSettings CoreSettings
        {
            get { return _CoreSettings; }
        }

        public override bool HasSettings
        {
            get { return true; }
        }
        #endregion

        #region Data Members
        private CoreCatalog catalog;
        #endregion

        #region Constructor
        public CorePlugin(Host host) : base(host)
        {
            LoadSettings();
            catalog = new CoreCatalog(this);
        }
        #endregion

        #region Public Methods
        public override void UpdateDatabase()
        {
            CoreNotifier coreNotifier = new CoreNotifier();
                Host.AddNotifier(coreNotifier);

            // TODO: Add items here (copy code from CoreCatalog)
            catalog.LoadItems();

            Host.AddAction(new Actions.Run(this));
            Host.AddAction(new Actions.Launch(this));
            Host.AddAction(new Actions.Reveal(this));
            Host.AddAction(new Actions.RevealTarget(this));
            Host.AddAction(new Actions.Display(this));
            Host.AddAction(new Actions.CopyToClipboard(this));
            Host.AddAction(new Actions.AppendTo(this));
            Host.AddAction(new Actions.PrependTo(this));
        }

        public override void ExtendItem(Item item)
        {
            if (item.Name.Equals("Computer", StringComparison.InvariantCultureIgnoreCase) ||
                item.Name.Equals("My Computer", StringComparison.InvariantCultureIgnoreCase))
            {
                item.Description = String.Empty;
                item.UpdateDrilledItems -= computerItem_UpdateDrilledItems;
                item.UpdateDrilledItems += computerItem_UpdateDrilledItems;
                HoldKeys[System.Windows.Forms.Keys.OemQuestion] = item;
            }
        }

        public override bool ShowSettings()
        {
            Settings settings = new Settings();
            settings.ShowDialog();

            return true;
        }

        public override void SaveSettings()
        {
            Host.DataManager.SaveSettings(this, CoreSettings);
        }

        public static bool IsTextFile(Item item)
        {
            if (!item.Properties.ContainsKey("Path"))
                return false;

            string path = item.Properties["Path"].ToString();
            string ext = Path.GetExtension(path.ToLower());
            return CoreSettings.TextFileExtensions.Contains(ext);
        }
        #endregion

        #region Private Methods
        private string GetCommonProgramPath()
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders");
            return key.GetValue("Common Programs").ToString();
        }

        private void LoadSettings()
        {
            _CoreSettings = Host.DataManager.LoadSettings<CoreSettings>(this);
            if (_CoreSettings == null)
            {
                _CoreSettings = new CoreSettings();
                CoreSettings.TextFileExtensions.Add(".txt");

                IndexFolder startMenuFolder = new IndexFolder(Environment.SpecialFolder.StartMenu);
                startMenuFolder.Extensions.Add(new IndexExtension(".lnk"));
                CoreSettings.IndexFolders.Add(startMenuFolder);
            }
        }
        #endregion

        #region Event Handlers
        private void computerItem_UpdateDrilledItems(Item item, List<ILookupItem> drilledItems)
        {
            catalog.ComputerItem_UpdateDrilledItems(item, drilledItems);
        }
        #endregion
    }
}
